package com.zjl.controller;


import com.zjl.anno.Log;
import com.zjl.pojo.Dept;
import com.zjl.pojo.Result;
import com.zjl.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName DeptController
 * @Description TODO
 * @Author ZJL
 * @Date 2023/10/26 18:20
 * @Version 1.0
 **/
@Slf4j
@RestController
@RequestMapping("/depts")
public class DeptController {
    @Autowired
    private DeptService deptService;

    /**
     * 部门列表查询
     * @return
     */
    @Log
    @GetMapping
    public Result list() {
        log.info("部门列表查询");
        List<Dept> deptList = deptService.list();
        return Result.success(deptList);
    }

    /**
     * 删除部门
     * @return id
     */
    @Log
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) throws Exception {
        //日志
        log.info("根据id删除部门",id);

        //根据部门id删除部门信息
        Integer result = deptService.delete(id);
        if (result>0){
            return Result.success();
        }else {
            return Result.error("删除失败");
        }

    }

    /**
     * 添加部门
     * @param dept
     * @return
     */
    @Log
    @PostMapping
    public Result add(@RequestBody Dept dept){
        //日志
        log.info("新增部门：{}",dept);
        Integer add = deptService.add(dept);
        if (add>0){
            return Result.success();
        }else {
            return Result.error("新增部门失败");
        }

    }

    /**
     * 根据id查询部门
     * @param id
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        //日志
        log.info("根据id部门：{}",id);
        Dept dept = deptService.getById(id);
        return Result.success(dept);
    }

    /**
     * 修改部门
     * @param dept
     */
    @Log
    @PutMapping
    public Result update(@RequestBody Dept dept){
        //日志
        log.info("修改部门：{}",dept);
        deptService.update(dept);
        return Result.success();
    }


}
